package com.tianbo.elfii.client.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class WlanCustomConnectDialog extends JPanel implements DocumentListener, ItemListener, FocusListener{

	public WlanAP connectingAP;
	
	public JButton confirmBtn;
	public JButton cancelBtn;
	
    BufferedImage bgImg = null;
	
    public JLabel titleLabel = null;
    
    public JTextField ssidInputField;
    
    public JTextField pwInputField;
    public JTextField textField;
    public JPasswordField passwordField;
    
    public JCheckBox pwVisibilityCheckBox;
    public ImageIcon pwVisibleImg = null;
    public ImageIcon pwUnvisibleImg = null;
    
    public JComboBox encryptComboBox;
    public boolean isEncryptFlag = false;
    
    public String titleStr = "连接到隐藏的Wi-Fi";
    public String apSSIDStr = " SSID";
    public String apEncryptStr = "加密类型";
    public String apPwStr = " 密 码";
    public String pwVisibility = "显示密码";
    
    public String pwNotMatchStr = "密码不正确";
    public String otherFailedStr = "连接失败 请重试";
    
    public boolean isPwMatch = true;
    
    public Font titleStrFont;
    
    public ElfiiCommonKeyboard keyboard = null;
    
	public WlanCustomConnectDialog(){
		
		titleStrFont = new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,24);
	}
	
	public void init(){
		this.setLayout(null);
		initImg();
		initBtn();
		initCheckBox();
		initSSIDInputField();
		initPasswordInputField(isPwMatch,"");

		initEncryptComboBox();
	}
	
	public void init(boolean isPwMatch,String errorPw){
		
		this.setLayout(null);
		initImg();
		initBtn();
		initCheckBox();
		initSSIDInputField();
		
		initPasswordInputField(isPwMatch,errorPw);

		initEncryptComboBox();
		
	}

	public void initImg(){
        File f;
        try {
            f=new File("resource-common/images/config/wlan/ss_03.png");
            bgImg=ImageIO.read(f);
            
            f=new File("resource-common/images/config/ss_06.png");
            pwUnvisibleImg = new ImageIcon(ImageIO.read(f));
            
            f=new File("resource-common/images/config/ss_07.png");
            pwVisibleImg = new ImageIcon(ImageIO.read(f));
            
        } catch(Exception e) {System.out.print(e);}
	}
	
	public void initBtn(){
		confirmBtn = new JButton();
		cancelBtn = new JButton();
		
		confirmBtn.setIcon(new ImageIcon("resource-common/images/login/sssss06.png"));
		cancelBtn.setIcon(new ImageIcon("resource-common/images/login/sssss07.png"));
		
		cancelBtn.setContentAreaFilled(false);
		cancelBtn.setBorder(null);
		cancelBtn.setFocusPainted(false);
		
		confirmBtn.setContentAreaFilled(false);
		confirmBtn.setBorder(null);
		confirmBtn.setFocusPainted(false);

		confirmBtn.setBounds(100, 220, 141, 44);
		cancelBtn.setBounds(304, 220, 141, 44);
		
		this.add(confirmBtn);
		this.add(cancelBtn);
		
		confirmBtn.setEnabled(false);
	}
	
	public void initSSIDInputField(){
		ssidInputField = new JTextField(20);
		
		this.add(ssidInputField);

		ssidInputField.setBorder(null);
		ssidInputField.setBounds(185, 95, 200, 22);
		ssidInputField.setVisible(true);
		
		if(connectingAP.connectRetFlag == 1){
			ssidInputField.setBorder(BorderFactory.createLineBorder(ElfiiCommonConstant.Elfii_Red));
		}
		
		ssidInputField.getDocument().addDocumentListener(this);
		ssidInputField.addFocusListener(this);
		ssidInputField.setText(connectingAP.getSSID());
	}
	
	public void initEncryptComboBox(){
		encryptComboBox = new JComboBox();
		this.add(encryptComboBox);
		encryptComboBox.setBounds(185,125,200,22);
		encryptComboBox.setBackground(Color.white);
		encryptComboBox.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,13));
		encryptComboBox.setBorder(null);
		encryptComboBox.addItem("不加密");
		encryptComboBox.addItem("加密");
		

		pwInputField.setEnabled(false);
		pwVisibilityCheckBox.setEnabled(false);
		
		encryptComboBox.addItemListener(this);
		
	}
	
	public void initPasswordInputField(boolean isPwMatch,String errorPw){
		
		this.isPwMatch = isPwMatch;
		
		textField = new JTextField(20);
		passwordField = new JPasswordField(20);
		
		this.add(textField);
		
		this.add(passwordField);

		textField.setBorder(null);
		textField.setBounds(185, 155, 200, 22);
		textField.setVisible(false);
		
		passwordField.setBorder(null);
		passwordField.setBounds(185, 155, 200, 22);
		passwordField.setVisible(false);
		
		if(pwVisibilityCheckBox.isSelected()){
			pwInputField = textField;
		}
		else{
			pwInputField = passwordField;
		}
		
		pwInputField.setVisible(true);
		pwInputField.setFocusable(true);
		
		
		if(connectingAP.connectRetFlag == 3){
			pwInputField.setBorder(BorderFactory.createLineBorder(ElfiiCommonConstant.Elfii_Red));
//			pwInputField.setText(errorPw);
		}
		
		textField.getDocument().addDocumentListener(this);
		passwordField.getDocument().addDocumentListener(this);
		
		textField.addFocusListener(this);
		passwordField.addFocusListener(this);
		
		pwInputField.setText(connectingAP.getPassword());
	}
	
	public void initCheckBox(){
		
		pwVisibilityCheckBox = new JCheckBox();
		pwVisibilityCheckBox.setIcon(pwUnvisibleImg);
		
		pwVisibilityCheckBox.setContentAreaFilled(false);
		
		this.add(pwVisibilityCheckBox);
		pwVisibilityCheckBox.setBounds(182, 180, 80, 20);
		
		pwVisibilityCheckBox.setRolloverEnabled(false);
		
		pwVisibilityCheckBox.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				// TODO Auto-generated method stub
				boolean tmpFlag = isPwMatch;
				
				String pw = pwInputField.getText();
				pwInputField.setVisible(false);
				
				if(pwVisibilityCheckBox.isSelected()){
					pwVisibilityCheckBox.setIcon(pwVisibleImg);
					pwInputField = textField;
				}
				else{
					pwVisibilityCheckBox.setIcon(pwUnvisibleImg);
					pwInputField = passwordField;
				}
				

				
				if(keyboard!=null){
					keyboard.setTextField(pwInputField, 20);
				}
				
				pwInputField.setVisible(true);
				pwInputField.setText(pw);
				pwInputField.setFocusable(true);
				
				if(!tmpFlag){
					pwInputField.setBorder(BorderFactory.createLineBorder(ElfiiCommonConstant.Elfii_Red));
					isPwMatch = tmpFlag;
				}
			}
			
		});
	}
	
	public void setRelatedKeyboard(ElfiiCommonKeyboard keyboard){
		this.keyboard = keyboard;
	}
	
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
      
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawImage(bgImg,null,0,0);

        g2.setColor(Color.white);
        
        g2.setFont(titleStrFont);
    
        g2.drawString(titleStr, 170, 55);
        
        g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,14));
        
        g2.drawString(apSSIDStr, 80, 110);
        g2.drawString(apEncryptStr, 80, 140);
        g2.drawString(apPwStr, 80, 170);
        
        g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,12));
        g2.drawString(pwVisibility,205,195);
        
        if(connectingAP.connectRetFlag == 1){
            g2.setColor(ElfiiCommonConstant.Elfii_Red);
            g2.drawString(pwNotMatchStr,400,170);
        }
        else if(connectingAP.connectRetFlag == 3){
            g2.setColor(ElfiiCommonConstant.Elfii_Red);
            g2.drawString(pwNotMatchStr,400,170);
        }
        else if(connectingAP.connectRetFlag != -1){
            g2.setColor(ElfiiCommonConstant.Elfii_Red);
            g2.drawString(otherFailedStr,400,170);
        }
    }


	@Override
	public void insertUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
		if(!isPwMatch){
			if(connectingAP.connectRetFlag==3){

				pwInputField.setBorder(null);
			}
			else{
				ssidInputField.setBorder(null);
			}
			isPwMatch = true;
			connectingAP.connectRetFlag=-1;
			this.repaint();
		}
		
		if(ssidInputField.getText().trim().equals("")){
			confirmBtn.setEnabled(false);
		}
		else if(!isEncryptFlag){
			confirmBtn.setEnabled(true);
		}
		else{
			if(pwInputField.getText().trim().equals("")){
				confirmBtn.setEnabled(false);
			}
			else{
				confirmBtn.setEnabled(true);
			}
		}
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
		if(!isPwMatch){
			if(connectingAP.connectRetFlag==3){

				pwInputField.setBorder(null);
			}
			else{
				ssidInputField.setBorder(null);
			}
			isPwMatch = true;
			connectingAP.connectRetFlag=-1;
			this.repaint();
		}
		
		if(ssidInputField.getText().trim().equals("")){
			confirmBtn.setEnabled(false);
		}
		else if(!isEncryptFlag){
			confirmBtn.setEnabled(true);
		}
		else{
			if(pwInputField.getText().trim().equals("")){
				confirmBtn.setEnabled(false);
			}
			else{
				confirmBtn.setEnabled(true);
			}
		}
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame w = new JFrame();
		w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		w.setUndecorated(true); //
		w.setSize(545, 272);
//		w.show();
		WlanCustomConnectDialog dialog = new WlanCustomConnectDialog();
		dialog.setBounds(0, 0, 545, 272);
		dialog.init();
		w.setContentPane(dialog);
		w.show();
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		if(e.getStateChange() == ItemEvent.SELECTED){
			if(e.getItem().toString().equals("加密")){
				pwInputField.setEnabled(true);
				pwVisibilityCheckBox.setEnabled(true);
				isEncryptFlag = true;
			}
			else{
				pwInputField.setEnabled(false);
				pwVisibilityCheckBox.setEnabled(false);
				isEncryptFlag = false;
			}
			
			if(ssidInputField.getText().trim().equals("")){
				confirmBtn.setEnabled(false);
			}
			else if(!isEncryptFlag){
				confirmBtn.setEnabled(true);
			}
			else{
				if(pwInputField.getText().trim().equals("")){
					confirmBtn.setEnabled(false);
				}
				else{
					confirmBtn.setEnabled(true);
				}
			}
		}
	}

	@Override
	public void focusGained(FocusEvent e) {
		// TODO Auto-generated method stub
		if(keyboard!=null){
			keyboard.setTextField((JTextField)e.getComponent(),30);
		}
	}

	@Override
	public void focusLost(FocusEvent e) {
		// TODO Auto-generated method stub
		if(keyboard!=null){
			keyboard.setTextField(null,30);
		}
	}

}
